package practice;

import practice.domain.Person;

import java.lang.reflect.Constructor;

public class 通过反射操作构造方法 {
    public static void main(String[] args) throws Exception{
        // 得到class对象
        Class clazz = Person.class;

        // 无参构造方法
        Constructor constructor1 = clazz.getConstructor();
        constructor1.setAccessible(true);
        Person person1 = (Person) constructor1.newInstance();
        System.out.println(person1);

        // 全参构造方法
        Constructor constructor2 = clazz.getConstructor(String.class, Integer.class);
        constructor2.setAccessible(true);
        Person person2 = (Person) constructor2.newInstance("shibixiu", 24);
        System.out.println(person2);

        //单参构造方法
        Constructor constructor3 = clazz.getConstructor(String.class);
        constructor3.setAccessible(true);
        Person person3 = (Person) constructor3.newInstance("shibixiu");
        System.out.println(person3);


    }
}
